package service

import (
	"duoke/model"
	"duoke/utils/gconv"
	"fmt"
	"github.com/gin-gonic/gin"
)

var Statistics = new(statisticsService)

type statisticsService struct{}

func (s *statisticsService) CourseList(c *gin.Context, req *model.ReqList) (*[]model.StaticsCourseList, int64, error) {
	account, err := Request.Account(c)
	if err != nil {
		return nil, 0, err
	}
	where := fmt.Sprintf("a.acid=%d", account.Id)
	var m []model.StaticsCourseList
	if req.Type == "share" || req.Type == "collect" {
		var shareType int
		if req.Type == "share" {
			shareType = 1
		} else {
			shareType = 2
		}
		where = fmt.Sprintf("%s and type=%d and state=1 and from_id=%s", where, shareType, req.FromId)
		count, err := tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("course_share").
			Alias("a").Cols("user.sub_mch_id,user_id,nickname,phone,avatar,a.id").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			FindAndCount(&m)
		for k, item := range m {
			f := item.Number / 60
			if f < 1 {
				m[k].Number = 0
			} else {
				m[k].Number = gconv.Float64(fmt.Sprintf("%.2f", f))
			}
		}
		if err != nil {
			return nil, 0, err
		}
		return &m, count, nil
	}
	if req.Type == "buy" {
		where = fmt.Sprintf("%s and state=1 and list_id=%s", where, req.FromId)
		count, err := tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("course_order").Alias("a").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			Cols("a.sub_mch_id,user_id,nickname,phone,avatar,a.id").FindAndCount(&m)
		if err != nil {
			return nil, 0, err
		}
		return &m, count, nil
	}
	if req.Type == "number_of_learners" {
		where = fmt.Sprintf("%s and from_id=%s", where, req.FromId)
		count, err := tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("course_progress").Alias("a").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			Cols("user.sub_mch_id,user_id,nickname,phone,avatar,a.id").FindAndCount(&m)
		if err != nil {
			return nil, 0, err
		}
		return &m, count, nil
	}

	return nil, 0, nil
}

func (s *statisticsService) ShortVideoList(c *gin.Context, req *model.ReqList) (*[]model.StaticsCourseList,
	int64, error) {
	account, err := Request.Account(c)
	if err != nil {
		return nil, 0, err
	}
	where := fmt.Sprintf("a.acid=%d", account.Id)
	var m []model.StaticsCourseList
	var count int64
	if req.Type == "dz" || req.Type == "zf" {
		var shareType int
		if req.Type == "dz" {
			shareType = 1
		} else {
			shareType = 2
		}
		where = fmt.Sprintf("%s and share_type=%d and state=1 and from_id=%s", where, shareType, req.FromId)
		count, err = tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("share_user").
			Alias("a").Cols("user.sub_mch_id,user_id,nickname,phone,avatar,a.id").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			FindAndCount(&m)
		if err != nil {
			return nil, 0, err
		}
	}
	if req.Type == "buy" {
		where = fmt.Sprintf("%s and state=1 and short_video_id=%s", where, req.FromId)
		count, err = tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("short_order").Alias("a").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			Cols("a.sub_mch_id,user_id,nickname,phone,avatar,a.id").FindAndCount(&m)
		if err != nil {
			return nil, 0, err
		}
	}
	if req.Type == "watch" {
		where = fmt.Sprintf("%s and from_id=%s", where, req.FromId)
		count, err = tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("short_watch").Alias("a").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			Cols("user.sub_mch_id,user_id,nickname,phone,avatar,a.id").FindAndCount(&m)
		if err != nil {
			return nil, 0, err
		}
	}
	if req.Type == "watch_number" {
		where = fmt.Sprintf("%s and from_id=%s", where, req.FromId)
		count, err = tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("short_watch").Alias("a").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			Cols("user.sub_mch_id,user_id,nickname,phone,avatar,a.id,watch_number as number").FindAndCount(&m)
		if err != nil {
			return nil, 0, err
		}
	}
	for k, item := range m {
		if item.Number == 0 {
			m[k].Number = 1
		}
	}
	if req.Type == "watch_time" {
		where = fmt.Sprintf("%s and from_id=%s", where, req.FromId)
		count, err = tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("short_watch").Alias("a").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			Cols("user.sub_mch_id,user_id,nickname,phone,avatar,a.id,watch_time as number").FindAndCount(&m)
		if err != nil {
			return nil, 0, err
		}
		for k, item := range m {
			f := item.Number / 60
			if f < 1 {
				m[k].Number = 0
			} else {
				m[k].Number = gconv.Float64(fmt.Sprintf("%.2f", f))
			}
		}
	}
	return &m, count, nil
}
func (s *statisticsService) CourseVideo(c *gin.Context, req *model.ReqList) (*[]model.StaticsCourseList,
	int64, error) {
	account, err := Request.Account(c)
	if err != nil {
		return nil, 0, err
	}
	where := fmt.Sprintf("a.acid=%d", account.Id)
	var m []model.StaticsCourseList
	var count int64
	if req.Type == "share" || req.Type == "collect" {
		var shareType int
		if req.Type == "collect" {
			shareType = 1
		} else {
			shareType = 2
		}
		where = fmt.Sprintf("%s and type=%d and state=1 and from_id=%s", where, shareType, req.FromId)
		count, err = tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("course_share").
			Alias("a").Cols("user.sub_mch_id,user_id,nickname,phone,avatar,a.id").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			FindAndCount(&m)
		if err != nil {
			return nil, 0, err
		}
	}
	if req.Type == "buy" {
		where = fmt.Sprintf("%s and state=1 and list_id=%s", where, req.FromId)
		count, err = tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("course_order").Alias("a").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			Cols("a.sub_mch_id,user_id,nickname,phone,avatar,a.id").FindAndCount(&m)
		if err != nil {
			return nil, 0, err
		}
	}
	if req.Type == "watch" {
		where = fmt.Sprintf("%s and list_id=%s", where, req.FromId)
		count, err = tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("course_progress").Alias("a").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			Cols("user.sub_mch_id,user_id,nickname,phone,avatar,a.id").
			FindAndCount(&m)
		if err != nil {
			return nil, 0, err
		}
	}
	if req.Type == "watch_number" {
		where = fmt.Sprintf("%s and list_id=%s", where, req.FromId)
		count, err = tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("course_progress").Alias("a").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			Cols("user.sub_mch_id,user_id,nickname,phone,avatar,a.id,watch_number as number").FindAndCount(&m)
		if err != nil {
			return nil, 0, err
		}
	}
	for k, item := range m {
		if item.Number == 0 {
			m[k].Number = 1
		}
	}
	if req.Type == "watch_time" {
		where = fmt.Sprintf("%s and list_id=%s", where, req.FromId)
		count, err = tableCommon.QueryListWithPageV2(where, req.PageNo, req.PageSize).
			Table("course_progress").Alias("a").
			Join("LEFT", "user", "a.user_id=user.id").OrderBy("a.id desc").
			Cols("user.sub_mch_id,user_id,nickname,phone,avatar,a.id,watch_time as number").FindAndCount(&m)
		if err != nil {
			return nil, 0, err
		}
		for k, item := range m {
			f := item.Number / 60
			if f < 1 {
				m[k].Number = 0
			} else {
				m[k].Number = gconv.Float64(fmt.Sprintf("%.2f", f))
			}
		}
	}
	return &m, count, nil

}
